﻿Public NotInheritable Class Settings
    Implements IDictionary(Of String, Object),  _
               ICollection(Of KeyValuePair(Of String, Object)),  _
               IEnumerable(Of KeyValuePair(Of String, Object)),  _
               IDictionary, ICollection, IEnumerable

    Private Dictionary As New Dictionary(Of String, Object)

#Region "Add"

    Private Sub Add1(ByVal item As System.Collections.Generic.KeyValuePair(Of String, Object)) Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Object)).Add
        Me.Add(item.Key, item.Value)
    End Sub

    Public Sub Add(ByVal key As String, ByVal value As Object) Implements System.Collections.Generic.IDictionary(Of String, Object).Add
        Dictionary.Add(key, value)
    End Sub

    Private Sub Add2(ByVal key As Object, ByVal value As Object) Implements System.Collections.IDictionary.Add
        If Not TypeOf key Is String Then
            Throw New InvalidCastException("key")
        End If
        Me.Add(CStr(key), value)
    End Sub

#End Region

#Region "Clear"

    Public Sub Clear() Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Object)).Clear
        Dictionary.Clear()
    End Sub

    Private Sub Clear1() Implements System.Collections.IDictionary.Clear
        Me.Clear()
    End Sub

#End Region

#Region "Contains"

    Public Function Contains(ByVal key As String) As Boolean
        Return Dictionary.ContainsKey(key)
    End Function

    Private Function Contains1(ByVal item As System.Collections.Generic.KeyValuePair(Of String, Object)) As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Object)).Contains
        Return Dictionary.ContainsKey(item.Key) AndAlso Dictionary.ContainsValue(item.Value)
    End Function

    Private Function Contains2(ByVal key As Object) As Boolean Implements System.Collections.IDictionary.Contains
        If Not TypeOf key Is String Then
            Throw New ArgumentException("key")
        End If
        Return Me.Contains(CStr(key))
    End Function

    Private Function ContainsKey(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Object).ContainsKey
        Return Me.Contains(key)
    End Function

#End Region

#Region "Count"

    Public ReadOnly Property Count() As Integer Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Object)).Count
        Get
            Return Dictionary.Count
        End Get
    End Property

    Private ReadOnly Property Count1() As Integer Implements System.Collections.ICollection.Count
        Get
            Return Me.Count
        End Get
    End Property

#End Region

#Region "Remove"

    Private Function Remove1(ByVal item As System.Collections.Generic.KeyValuePair(Of String, Object)) As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Object)).Remove
        If Dictionary.ContainsKey(item.Key) AndAlso Dictionary.ContainsValue(item.Value) Then
            Return Me.Remove(item.Key)
        Else
            Return False
        End If
    End Function

    Public Function Remove(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Object).Remove
        Return Dictionary.Remove(key)
    End Function

    Private Sub Remove2(ByVal key As Object) Implements System.Collections.IDictionary.Remove
        If Not TypeOf key Is String Then
            Throw New ArgumentException("key")
        End If
        Me.Remove(CStr(key))
    End Sub

#End Region

#Region "Keys"

    Private ReadOnly Property Keys1() As System.Collections.Generic.ICollection(Of String) Implements System.Collections.Generic.IDictionary(Of String, Object).Keys
        Get
            Return Dictionary.Keys
        End Get
    End Property

    Public ReadOnly Property Keys() As System.Collections.ICollection Implements System.Collections.IDictionary.Keys
        Get
            Return Dictionary.Keys
        End Get
    End Property

#End Region

#Region "Item"

    Default Public Overloads Property Item(ByVal key As String) As Object Implements System.Collections.Generic.IDictionary(Of String, Object).Item
        Get
            Return Dictionary.Item(key)
        End Get
        Set(ByVal value As Object)
            Dictionary.Item(key) = value
        End Set
    End Property

    Default Protected Overloads Property Item(ByVal key As Object) As Object Implements System.Collections.IDictionary.Item
        Get
            Return Dictionary.Item(CStr(key))
        End Get
        Set(ByVal value As Object)
            Dictionary.Item(CStr(key)) = value
        End Set
    End Property

#End Region

    Public Function GetValue(Of T)(ByVal key As String, ByVal defaultValue As T) As T
        Dim v As T
        If Me.TryGetValue(key, v) Then
            Return v
        Else
            Return defaultValue
        End If
    End Function

    Public Function GetValue(Of T)(ByVal key As String) As T
        Dim v As T
        If Me.TryGetValue(key, v) Then
            Return v
        Else
            Return Nothing
        End If
    End Function

    Public Sub SetValue(ByVal key As String, ByVal value As Object)
        If Me.Contains(key) Then
            Me(key) = value
        Else
            Me.Add(key, value)
        End If
    End Sub


#Region "TryGetValue"

    Public Function TryGetValue(Of T)(ByVal key As String, ByRef value As T) As Boolean
        Dim v As Object = Nothing
        If Not Dictionary.TryGetValue(key, v) Then
            Return False
        End If

        If TypeOf v Is T Then
            value = DirectCast(v, T)
            Return True
        End If

        Return False
    End Function

    Private Function TryGetValue1(ByVal key As String, ByRef value As Object) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Object).TryGetValue
        Return Dictionary.TryGetValue(key, value)
    End Function

#End Region

#Region "CopyTo"

    Private Sub CopyTo(ByVal array() As System.Collections.Generic.KeyValuePair(Of String, Object), ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Object)).CopyTo
        DirectCast(Dictionary, ICollection(Of KeyValuePair(Of String, Object))).CopyTo(array, arrayIndex)
    End Sub

    Private Sub CopyTo1(ByVal array As System.Array, ByVal index As Integer) Implements System.Collections.ICollection.CopyTo
        DirectCast(Dictionary, ICollection).CopyTo(array, index)
    End Sub

#End Region

#Region "IsReadOnly"

    Private ReadOnly Property IsReadOnly() As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Object)).IsReadOnly
        Get
            Return DirectCast(Dictionary, ICollection(Of KeyValuePair(Of String, Object))).IsReadOnly
        End Get
    End Property

    Private ReadOnly Property IsReadOnly1() As Boolean Implements System.Collections.IDictionary.IsReadOnly
        Get
            Return DirectCast(Dictionary, IDictionary).IsReadOnly
        End Get
    End Property

#End Region

#Region "Values"

    Private ReadOnly Property Values() As System.Collections.Generic.ICollection(Of Object) Implements System.Collections.Generic.IDictionary(Of String, Object).Values
        Get
            Return Dictionary.Values
        End Get
    End Property

    Private ReadOnly Property Values1() As System.Collections.ICollection Implements System.Collections.IDictionary.Values
        Get
            Return Dictionary.Values
        End Get
    End Property

#End Region

#Region "GetEnumerator"

    Private Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of System.Collections.Generic.KeyValuePair(Of String, Object)) Implements System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of String, Object)).GetEnumerator
        Return Dictionary.GetEnumerator
    End Function

    Private Function GetEnumerator1() As System.Collections.IDictionaryEnumerator Implements System.Collections.IDictionary.GetEnumerator
        Return Dictionary.GetEnumerator
    End Function

    Private Function GetEnumerator2() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        Return Dictionary.GetEnumerator
    End Function

#End Region

    Private ReadOnly Property IsSynchronized() As Boolean Implements System.Collections.ICollection.IsSynchronized
        Get
            Return DirectCast(Dictionary, ICollection).IsSynchronized
        End Get
    End Property

    Private ReadOnly Property SyncRoot() As Object Implements System.Collections.ICollection.SyncRoot
        Get
            Return DirectCast(Dictionary, ICollection).SyncRoot
        End Get
    End Property

    Private ReadOnly Property IsFixedSize() As Boolean Implements System.Collections.IDictionary.IsFixedSize
        Get
            Return DirectCast(Dictionary, IDictionary).IsFixedSize
        End Get
    End Property


End Class

